package model.data
{
	import flash.filesystem.File;

	public class Tales extends BaseData
	{
		private var map:Object;
		
		override protected function process(data:String):void
		{
			map ={};
			
			var letter:String;
			var rows:Array = data.split("\r\n");
			var matrix:String;
			
			for (var i:int=0;i<rows.length;i++)
			{
				var row:String = rows[i];
				
				if (i%13==0)
				{
					var rowParts:Array = row.split(" ");
					letter = rowParts[0];
					map[letter] = [];
					
					matrix = getMatrix(rowParts);
					
					continue;
				}
				
				map[letter].push(getRow(row,matrix));
			}
		}
		
		private function getMatrix(row:Array):String
		{
			if (row.length > 2)
				return row[2].charAt();
			else
				return null;
		}
		
		private function getRow(row:String, matrix:String):TaleRow
		{
			var name:String = getName(row,matrix);
			
			if (!matrix)
				matrix = row.substring(row.indexOf("(")+1,row.indexOf(")"));
			
			return new TaleRow(name,matrix);
		}
		
		private function getName(row:String, matrix:String):String
		{
			var name:String = row.substr(row.indexOf(" ")+1);
			
			if (!matrix)
				name = name.substring(0,name.indexOf("(")-1);
			return name;
		}
		
		override protected function get store():File
		{
			return super.store.resolvePath("tales.txt");
		}
	}
}